package com.webwork.lims.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webwork.lims.system.dataobject.User;
import com.webwork.lims.system.service.UserService;
import com.webwork.lims.util.RSA;
import com.webwork.lims.util.StrManager;

@Controller
@SessionAttributes("user")
public class LoginController {

	@Autowired
	private UserService userSve;

	@RequestMapping("/index")
	public void index() {
	}

	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String login(@RequestParam("userId") String userId, @RequestParam("password") String password,
			Map<String, Object> map, Model model, User user, BindingResult result) {
		if (StrManager.isEmpty(userId) || StrManager.isEmpty(password)) {
			map.put("tip", "帐号或者密码不能为空");
			return "reLogin";
		}
		User queryObj = new User();
		queryObj.setUserId(userId);
		queryObj.setPassword(RSA.encodeBase(password));
		List<User> users = userSve.query(queryObj);
		if (users != null && users.size() == 1) {
			map.put("user", users.get(0));
			model.addAttribute("user", users.get(0));
			return "main";
		} else {
			map.put("tip", "帐号或者密码错误");
			return "reLogin";
		}
	}

}
